package com.iqiyi.pps.epg.core.utils;

import com.alibaba.fastjson.JSONObject;
import com.iqiyi.pps.epg.core.model.program.ProgramData;
import com.iqiyi.pps.epg.core.model.ticket.TicketStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by karkarrotxu on 2017/9/6.
 */
public class ApiTicketSysUtil {
    private static Logger logger = LoggerFactory.getLogger(ApiSnapshotUtil.class);
    public static String TICKET_ADD_URL = "http://epg.lego.iqiyi.com/api/ticket/data/add";
    public static String TICKET_INTERRUPT_URL = "http://epg.lego.iqiyi.com/api/ticket/data/interrupt";
    public static int TICKET_TYPE_LIVE = 1;
    public static String DEFAULT_EXECUTOR = "yaoyihua@qiyi.com";

    private ApiTicketSysUtil() {
    }

    public static void sendLiveNotify(ProgramData programData,long cQipuId, String reporter) {
        JSONObject detail = new JSONObject();
        detail.put("startPlayTime", DateTimeUtil.transLongFormat(programData.getStartPlayTime().getTime()));
        detail.put("stopPlayTime", DateTimeUtil.transLongFormat(programData.getStopPlayTime().getTime()));
        detail.put("bossStatus", programData.getBossStatus());
        detail.put("episodeGrade", programData.getEpisodeGrade());
        detail.put("test", programData.getTest());
        detail.put("freeStatus", programData.getFreeStatus());
        detail.put("programName", programData.getPname());
        detail.put("qipuId", programData.getQipuId());
        detail.put("channelQipuId", cQipuId);
        JSONObject object = new JSONObject();
        object.put("reporter", reporter == null ? Constants.SYSTEM_USER_SIGN : reporter);
        object.put("executor", DEFAULT_EXECUTOR);
        object.put("dataType", TICKET_TYPE_LIVE);
        object.put("resourceId", programData.getQipuId());
        object.put("title", "《" + programData.getPname() + "》临时直播检查");
        object.put("status", TicketStatus.WAIT_DEAL.getStatusIndex());
        object.put("detail", detail.toJSONString());
        object.put("oaDocNo", programData.getOaDocNo());
        object.put("oaDocUrl", programData.getOaDocUrl());
        object.put("oaDocEditorStatus", programData.getOaDocEditorStatus());
        object.put("deadline", DateTimeUtil.transLongFormat(programData.getStartPlayTime().getTime()));
        Map<String, String> params = new HashMap<String, String>();
        params.put("data", object.toJSONString());
        JSONObject ret = ApiHttpRequestUtil.doPost(TICKET_ADD_URL, null, params);
        logger.info("[sendLiveNotify]" + ret.toJSONString());
    }

    public static void sendLiveNotifyCancel(ProgramData programData) {
        JSONObject object = new JSONObject();
        object.put("dataType", TICKET_TYPE_LIVE);
        object.put("resourceId", programData.getQipuId());
        object.put("status", TicketStatus.INTERRUPT.getStatusIndex());
        Map<String, String> params = new HashMap<String, String>();
        params.put("data", object.toJSONString());
        JSONObject ret = ApiHttpRequestUtil.doPost(TICKET_INTERRUPT_URL, null, params);
        logger.info("[sendLiveNotifyCancel]" + ret.toJSONString());
    }
}
